<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="品名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入品名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中文名称" prop="nameDesc">
        <el-input
          v-model="queryParams.nameDesc"
          placeholder="请输入中文名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="方法学" prop="testMethod">
        <el-select v-model="queryParams.testMethod" placeholder="请选择方法学" clearable size="small">
          <el-option
            v-for="dict in testMethodOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="英文缩写" prop="shortName">
        <el-input
          v-model="queryParams.shortName"
          placeholder="请输入英文缩写"
          clearable
          maxlength="2"
          show-word-limit
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货号简称" prop="itemShortName">
        <el-input
          v-model="queryParams.itemShortName"
          placeholder="请输入货号简称"
          clearable
          maxlength="3"
          show-word-limit
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:project:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:project:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:project:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:project:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="品名" align="center" prop="name" width="90"/>
      <el-table-column label="中文名称" show-overflow-tooltip align="center" prop="nameDesc"/>
      <el-table-column label="方法学" align="center" prop="testMethod" :formatter="testMethodFormat"/>
      <el-table-column label="英文缩写" align="center" prop="shortName" width="80"/>
      <el-table-column label="货号简称" align="center" prop="itemShortName" width="90"/>
      <el-table-column label="效期" align="center" prop="effectivePeriod" width="80" :formatter="effectivePeriodFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:project:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:project:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-col :span="12">
            <el-form-item label="品名" prop="name">
              <el-input v-model="form.name" placeholder="请输入品名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中文名称" prop="nameDesc">
              <el-input v-model="form.nameDesc" placeholder="请输入中文名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文缩写" prop="shortName">
              <el-input v-model="form.shortName" maxlength="2"
                        show-word-limit placeholder="请输入英文缩写"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="货号简称" prop="itemShortName">
              <el-input v-model="form.itemShortName" maxlength="3"
                        show-word-limit placeholder="货号简称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="效期" prop="effectivePeriod">
              <el-select v-model="form.effectivePeriod" placeholder="请选择效期">
                <el-option
                  v-for="dict in effectivePeriodOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="方法学" prop="testMethod">
              <el-select v-model="form.testMethod" filterable placeholder="请选择方法学">
                <el-option
                  v-for="dict in testMethodOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册证号" prop="registrationNo">
              <el-input v-model="form.registrationNo" placeholder="请输入注册证号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产许可证号" prop="productionLicenseNo">
              <el-input v-model="form.productionLicenseNo" placeholder="请输入生产许可证号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item>
              <el-col :span="12">
                <el-form-item prop="ceFlag">
                  <el-checkbox v-model="form.ceFlag" label="标签CE标识"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item prop="mflag">
                  <el-checkbox v-model="form.mflag" label="批号M标识"/>
                </el-form-item>
              </el-col>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addProject, delProject, exportProject, getProject, listProject, updateProject} from "@/api/product/project";

export default {
  name: "Project",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 产品表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 效期字典
      effectivePeriodOptions: [],
      // 方法学字典
      testMethodOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        nameDesc: undefined,
        shortName: undefined,
        itemShortName: undefined,
        codeNo: undefined,
        effectivePeriod: undefined,
        creator: undefined,
        createtime: undefined,
        testMethod: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "品名不能为空", trigger: "blur"}
        ],
        nameDesc: [
          {required: true, message: "中文名称不能为空", trigger: "blur"}
        ],
        shortName: [
          {required: true, message: "英文缩写不能为空", trigger: "blur"}
        ], itemShortName: [
          {required: true, message: "货号简称不能为空", trigger: "blur"}
        ],
        effectivePeriod: [
          {required: true, message: "效期不能为空", trigger: "change"}
        ],
        testMethod: [
          {required: true, message: "方法学不能为空", trigger: "change"}
        ],
        registrationNo: [
          {required: true, message: "注册证号不能为空", trigger: "blur"}
        ],
        productionLicenseNo: [
          {required: true, message: "生产许可证号不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("expiredate_list").then(response => {
      this.effectivePeriodOptions = response.data;
    });
    this.getDicts("test_method").then(response => {
      this.testMethodOptions = response.data;
    });
  },
  methods: {
    /** 查询产品列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 效期字典翻译
    effectivePeriodFormat(row, column) {
      return this.selectDictLabel(this.effectivePeriodOptions, row.effectivePeriod);
    },
    // 方法学字典翻译
    testMethodFormat(row, column) {
      return this.selectDictLabel(this.testMethodOptions, row.testMethod);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: undefined,
        name: undefined,
        nameDesc: undefined,
        shortName: undefined,
        itemShortName: undefined,
        codeNo: undefined,
        effectivePeriod: undefined,
        creator: undefined,
        createtime: undefined,
        delFlag: undefined,
        testMethod: undefined,
        registrationNo: undefined,
        productionLicenseNo: undefined,
        mflag: false,
        ceFlag: false,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const projectId = row.projectId || this.ids
      getProject(projectId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.projectId != undefined) {
            updateProject(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addProject(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectIds = row.projectId || this.ids;
      this.$confirm('是否确认删除测试产品代码为"' + projectIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delProject(projectIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportProject(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function () {
      });
    }
  }
};
</script>
